'''
dialogs
Author: Alex Foran
'''

import pygame,gameroot,pane,loaders,math,dialogelements,webbrowser,titlescreen,os

'''
A dialog that contains a menu of options. Hitting cancel will close this at any time.
'''
class OptionsDialog (pane.Pane):
	def __init__(self,root):
		pane.Pane.__init__(self,root)
		#calculate values to always center this dialog
		self.xbox=(self.root.prefs["resolution"][0]-200)/2
		self.ybox=(self.root.prefs["resolution"][1]-120)/2
		#load cursor
		self.cursor=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		#add elements
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+2,"Settings",self.root.font24,self.cursor,pygame.color.Color("white"),self.option_settings))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+2+30,"Controls",self.root.font24,self.cursor,pygame.color.Color("white"),self.option_controls))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+2+60,"Help",self.root.font24,self.cursor,pygame.color.Color("white"),self.option_help))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+2+90,"Back",self.root.font24,self.cursor,pygame.color.Color("white"),self.option_back))
		self.focusedelement=self.elements[0]
	
	'''
	Show settings menu.
	'''
	def option_settings(self):
		self.dialog=SettingsDialog(self.root)
	
	'''
	Show controls menu.
	'''
	def option_controls(self):
		self.dialog=ControlsDialog(self.root)
	
	'''
	Navigate to help in the user's web browser.
	'''
	def option_help(self):
		#minimize this game to make everything nice
		pygame.display.iconify()
		#should open in a new tab
		webbrowser.open("http://agdoa.net/pew/")
	
	'''
	Closes this dialog.
	'''
	def option_back(self):
		self.done=True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None:
			if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
				self.done=True
				
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		#draw box
		surface.fill(pygame.color.Color("gray"),pygame.rect.Rect(self.xbox,self.ybox,200,120))
		surface.fill(pygame.color.Color("black"),pygame.rect.Rect(self.xbox+2,self.ybox+2,196,116))
		pane.Pane.repaint(self)

'''
Dialog that contains settings options. Pressing cancel at any time will open this dialog.
'''
class SettingsDialog(pane.Pane):
	'''
	Constructor.
	
	Params:
	root - A reference to the current gameroot.
	'''
	def __init__(self,root):
		pane.Pane.__init__(self,root)
		#centering calculations
		self.xbox=(self.root.prefs["resolution"][0]-400)/2
		self.ybox=(self.root.prefs["resolution"][1]-300)/2
		#load cursor
		self.cursor=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		#initial dictionary of resolutions - this should probably be elsewhere
		resdict=["640x480","720x480","800x600","1024x768","1152x864","1280x720","1280x768","1280x800","1360x768","1366x768","1440x900","1600x1200","1680x1050","1920x1080","1920x1200","2048x1152","2560x1600"]
		respos=0
		#attempt to display the current resolution as the default
		for i in range(len(resdict)):
			if resdict[i]==str(self.root.prefs["resolution"][0])+"x"+str(self.root.prefs["resolution"][1]):
				respos=i
				break
		#add elements
		self.elements.append(dialogelements.DialogClickSwitch(self,self.xbox+2,self.ybox+2,"Resolution:",self.root.font24,self.cursor,pygame.color.Color("white"),"resolution",resdict,respos))
		self.elements.append(dialogelements.DialogClickSwitch(self,self.xbox+2,self.ybox+32,"Fullscreen:",self.root.font24,self.cursor,pygame.color.Color("white"),"fullscreen",["Yes","No"],1 if not self.root.prefs["fullscreen"] else 0))
		#self.elements.append(dialogelements.DialogClickSwitch(self,self.xbox+2,self.ybox+62,"Use Alpha Blending:",self.root.font24,self.cursor,pygame.color.Color("white"),"usealphablending",["Yes","No"],1 if not self.root.prefs["usealphablending"] else 0))
		self.elements.append(dialogelements.DialogClickSwitch(self,self.xbox+2,self.ybox+62,"Always Show Cursor:",self.root.font24,self.cursor,pygame.color.Color("white"),"showcursor",["Yes","No"],1 if not self.root.prefs["showcursor"] else 0))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+240,"Save Changes",self.root.font24,self.cursor,pygame.color.Color("white"),self.acceptaction))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+270,"Cancel",self.root.font24,self.cursor,pygame.color.Color("white"),self.cancelaction))
		self.focusedelement=self.elements[len(self.elements)-1]
	
	'''
	Save all the settings into the prefs and activates them by reloading the title screen.
	'''
	def acceptaction(self):
		#get form items
		options=self.getkeyedelements()
		for option in options:
			key=option.getkey()
			value=option.getvalue()
			#parse resolution in a special case
			if key=="resolution":
				self.root.prefs["resolution"]=(int(value[0:value.find("x")]),int(value[value.find("x")+1:len(value)]))
			#everything else is boolean
			else:
				self.root.prefs[key]=(value=="Yes")
		self.root.saveprefs()
		#reconfigure resolution
		self.root.showdisplay()
		#reload title screen to show changes
		self.root.pane=titlescreen.TitleScreen(self.root)
	
	'''
	Closes this dialog.
	'''
	def cancelaction(self):
		self.done=True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None:
			if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
				self.done=True
	
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		surface.fill(pygame.color.Color("gray"),pygame.rect.Rect(self.xbox,self.ybox,400,300))
		surface.fill(pygame.color.Color("black"),pygame.rect.Rect(self.xbox+2,self.ybox+2,396,296))
		pane.Pane.repaint(self)

'''
A dialog that allows a use to customize controls. Pressing cancel at any time will close this dialog.
'''
class ControlsDialog (pane.Pane):
	def __init__(self,root):
		pane.Pane.__init__(self,root)
		#centering calculations
		self.xbox=(self.root.prefs["resolution"][0]-350)/2
		self.ybox=(self.root.prefs["resolution"][1]-480)/2
		#load cursor
		self.cursor=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		#add elements
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+4,"Thrust",self.root.font24,self.cursor,pygame.color.Color("white"),"key_thrust",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+34,"Stabilize",self.root.font24,self.cursor,pygame.color.Color("white"),"key_stabilize",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+64,"Rotate Left",self.root.font24,self.cursor,pygame.color.Color("white"),"key_rotateleft",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+94,"Rotate Right",self.root.font24,self.cursor,pygame.color.Color("white"),"key_rotateright",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+124,"Fire",self.root.font24,self.cursor,pygame.color.Color("white"),"key_mainfire",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+154,"Menu Up",self.root.font24,self.cursor,pygame.color.Color("white"),"key_up",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+184,"Menu Down",self.root.font24,self.cursor,pygame.color.Color("white"),"key_down",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+214,"Menu Left",self.root.font24,self.cursor,pygame.color.Color("white"),"key_left",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+244,"Menu Right",self.root.font24,self.cursor,pygame.color.Color("white"),"key_right",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+274,"Menu Accept",self.root.font24,self.cursor,pygame.color.Color("white"),"key_accept",150))
		self.elements.append(dialogelements.DialogKeyEntry(self,self.xbox+2,self.ybox+304,"Menu Cancel",self.root.font24,self.cursor,pygame.color.Color("white"),"key_cancel",150))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+420,"Save Changes",self.root.font24,self.cursor,pygame.color.Color("white"),self.acceptaction))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+450,"Cancel",self.root.font24,self.cursor,pygame.color.Color("white"),self.cancelaction))
		self.focusedelement=self.elements[0]
	
	'''
	Saves values into prefs and closes this dialog.
	'''
	def acceptaction(self):
		options=self.getkeyedelements()
		for option in options:
			self.root.prefs[option.getkey()]=option.getvalue()
		self.root.saveprefs()
		self.done=True
	
	'''
	Closes this dialog.
	'''
	def cancelaction(self):
		self.done=True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None:
			if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
				self.done=True
	
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		surface.fill(pygame.color.Color("gray"),pygame.rect.Rect(self.xbox,self.ybox,350,480))
		surface.fill(pygame.color.Color("black"),pygame.rect.Rect(self.xbox+2,self.ybox+2,346,476))
		pane.Pane.repaint(self)

'''
A dialog that allows a user to join a network game.
'''
class LoginDialog (pane.Pane):
	'''
	Constructor.
	
	Params:
	root - A reference to the current gameroot.
	destinationfunction - The function to call after the settings have been saved.
	'''
	def __init__(self,root,destinationfunction):
		pane.Pane.__init__(self,root)
		self.xbox=(self.root.prefs["resolution"][0]-400)/2
		self.ybox=(self.root.prefs["resolution"][1]-220)/2
		self.cursor=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		self.destinationfunction=destinationfunction
		#load default values from prefs if they exist, otherwise...
		if "network_playername" in self.root.prefs:
			playername=self.root.prefs["network_playername"];
			host=self.root.prefs["network_host"];
			user=self.root.prefs["network_user"];
			password=self.root.prefs["network_password"];
		#...use some default values
		else:
			#this will be the computer name, but only on windows and some distros of linux. sorry uphoff.
			playername=os.getenv("COMPUTERNAME")
			host="localhost"
			user=""
			password=""
		#add elements
		self.elements.append(dialogelements.DialogInputField(self,self.xbox+2,self.ybox+4,"Player Name:",self.root.font24,self.cursor,pygame.color.Color("white"),"network_playername",180,playername))
		self.elements.append(dialogelements.DialogInputField(self,self.xbox+2,self.ybox+34,"Host:",self.root.font24,self.cursor,pygame.color.Color("white"),"network_host",180,host))
		self.elements.append(dialogelements.DialogInputField(self,self.xbox+2,self.ybox+64,"User:",self.root.font24,self.cursor,pygame.color.Color("white"),"network_user",180,user))
		self.elements.append(dialogelements.DialogInputField(self,self.xbox+2,self.ybox+94,"Password:",self.root.font24,self.cursor,pygame.color.Color("white"),"network_password",180,password))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+154,"Play",self.root.font24,self.cursor,pygame.color.Color("white"),self.acceptaction))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+184,"Cancel",self.root.font24,self.cursor,pygame.color.Color("white"),self.cancelaction))
		self.focusedelement=self.elements[0]
	
	'''
	Saves the values into the prefs, and takes the user to the specified place.
	'''
	def acceptaction(self):
		for element in self.getkeyedelements():
			self.root.prefs[element.getkey()]=element.getvalue()
		self.root.saveprefs()
		self.root.pane=self.destinationfunction(self.root,self.root.prefs["network_playername"])
	
	'''
	Closes this dialog.
	'''
	def cancelaction(self):
		self.done=True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None:
			if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
				self.done=True
	
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		surface.fill(pygame.color.Color("gray"),pygame.rect.Rect(self.xbox,self.ybox,400,220))
		surface.fill(pygame.color.Color("black"),pygame.rect.Rect(self.xbox+2,self.ybox+2,396,214))
		pane.Pane.repaint(self)

'''
Dialog to serve as a simple pause menu.
'''
class PauseDialog(pane.Pane):
	'''
	Constructor.
	
	Params:
	root - A refernce to the current gameroot.
	'''
	def __init__(self,root):
		pane.Pane.__init__(self,root)
		#centering calculation
		self.xbox=(self.root.prefs["resolution"][0]-200)/2
		self.ybox=(self.root.prefs["resolution"][1]-90)/2
		#load cursor
		self.cursor=loaders.load_image_alpha(gameroot.respath+"dialogs/arrow-alpha.png")
		#add elements
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+4,"Continue",self.root.font24,self.cursor,pygame.color.Color("white"),self.continueaction))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+34,"Main Menu",self.root.font24,self.cursor,pygame.color.Color("white"),self.mainmenuaction))
		self.elements.append(dialogelements.DialogMenuItem(self,self.xbox+2,self.ybox+64,"Quit",self.root.font24,self.cursor,pygame.color.Color("white"),self.quitaction))
		self.focusedelement=self.elements[0]
	
	'''
	Closes this dialog.
	'''
	def continueaction(self):
		self.done=True
	
	'''
	Returns the game to the title screen.
	'''
	def mainmenuaction(self):
		self.root.pane=titlescreen.TitleScreen(self.root)
	
	'''
	Marks the game as done, supposedly closing the program.
	'''
	def quitaction(self):
		self.root.pane.done=True
	
	'''
	Update.
	'''
	def update(self):
		pane.Pane.update(self)
		if self.dialog==None:
			if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
				self.done=True
	
	'''
	Paint.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		surface.fill(pygame.color.Color("gray"),pygame.rect.Rect(self.xbox,self.ybox,200,90))
		surface.fill(pygame.color.Color("black"),pygame.rect.Rect(self.xbox+2,self.ybox+2,196,86))
		pane.Pane.repaint(self)